package projectpractice;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DishPracticeServiceImpl extends DishesPracticeFileImpl {
    private Map<String, Dishes> dishesPractices;

    public void add(Dishes dishesPractice) {
        if (dishesPractices == null) {
            dishesPractices = new HashMap<String, Dishes>();
        }
        if (dishesPractice == null) {
            return;
        }
        if (dishesPractices.containsKey(dishesPractice.getId()) == false) {
            dishesPractices.put(dishesPractice.getId(), dishesPractice);
            List<Dishes> dishesPracticeList = dishesPractices.values().stream().collect(Collectors.toList());
            store(dishesPracticeList);
        } else if (dishesPractices.containsKey(dishesPractice.getId())) {
            System.out.println("该菜品已存在，请重新添加");
        }
    }

    public List<Dishes> search(DishesPracticeParam param) {
        List<Dishes> dishesPracticeList = new ArrayList<Dishes>();
        Stream<Dishes> stream = dishesPracticeList.stream();
        if (param.getName() != null && !param.getName().equals("")) {
            stream = stream.filter(m ->
                    m.getName().indexOf(param.getName()) >= 0);
        }
        return stream.collect(Collectors.toList());
    }
}

